<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#afafaf"></uni-icons>
		<button type="primary" class="btn-login" @click="getUserInfo" >一键登录</button>
		<text class="tips-text">登录后尽享更多权益</text>
	</view>
</template>

<script>
	import {mapMutations,mapState} from 'vuex'
	export default {
		name:"my-login",
		computed:{
			// 使用辅助函数
			...mapState('my_user',['redirectInfo'])
		},
		data() {
			return {
				
			};
		},
		methods:{
			// 使用辅助函数
			...mapMutations('my_user',['updateUserInfo','updateUserToken','updateRedirctInfo']),
			// 获取用户信息
			async getUserInfo(){
				const res = await uni.getUserProfile({
					desc:'test'
				})
				this.updateUserInfo(res.userInfo)
				this.getToken(res)
			},
			// 获取用户token
			async getToken(e){
				const res = await uni.login().catch(err=>err)
				if(!res.errMsg||res.errMsg!=="login:ok") return uni.$showMsg('登录失败')
				// 写死token
				this.updateUserToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
				// __BUG__BUG__BUG__BUG__BUG__BUG__BUG__BUG__BUG__BUG接口死掉了，写死token为res.code
				// const query={
				// 	code:res.code,
				// 	encryptedData:e.encryptedData,
				// 	iv:e.iv,
				// 	rawData:e.rawData,
				// 	signature:e.signature,
				// }
				// console.log(query);
				// //发起请求
				// const kkk = await uni.$http.post('/api/public/v1/users/wxlogin',query)
				// console.log(kkk);
				// __BUG__BUG__BUG__BUG__BUG__BUG__BUG__BUG__BUG__BUG
				this.navigateBack()
			},
			// 如果又购物车页登录成功后跳转回购物车
			navigateBack(){
				if(this.redirectInfo && this.redirectInfo.openType==='switchTab'){
					uni.switchTab({
						url:this.redirectInfo.from,
						complete:()=>{
							this.updateRedirctInfo(null)
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.login-container{
		height: 750rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		position: relative;
		overflow: hidden;
		&::after{
			content: '';
			display: block;
			width: 100%;
			height: 40px;
			background-color: white;
			position: absolute;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translateY(50%);
		}
		.btn-login{
			width: 90%;
			border-radius: 100px;
			margin: 15px 0;
			background-color: #c00000;
		}
		.tips-text{
			font-size: 12px;
			color: gray;
		}
	}
</style>